package api;

import com.alibaba.fastjson.JSON;
import domain.Notice;
import service.NoticeService;
import service.UserService;
import utils.Response;
import utils.ResponseCode;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

public class GetNoticeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //分层：api：接受请求，提取请求参数，返回消息。service
        NoticeService noticeService=new NoticeService();
        UserService userService=new UserService();
        Response build=null;
        Notice notice=null;
        try {
           notice=noticeService.getRecNotice();//登陆成功，返回数据
           build=Response.build(ResponseCode.SUCCESS,notice) ;
        }catch (SQLException e){
            e.printStackTrace();//登录异常，返回成功消息
            build= Response.build(ResponseCode.ERROR_DB);
        }
        //第四部：判断为空，生成登录失败的消息对象
        //第五步：把消息对象转换层字符串，返回浏览器
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().write(JSON.toJSON(build).toString());

    }
    }

